#pragma once
#include <vector>
#include "MyMutex.h"



#define SIZE 6 //Number of bytes needed to redirect
#define MAX_PROCESSNUM 200
#define WMAPIHOOK	WM_USER+1986

enum FunNameID 
{
	eMessageBoxW,
	esendto,
	esend,
	elisten,
	ebind,
	econnect,
	erecv,
	erecvfrom,
	FunNum
};

enum ShareDllEvents
{
	seSend,
	seSendto,
	seListen,
	sebind,
	seconnect,
	serecv,
	serecvfrom,
	EventsNum
};


typedef struct FunAddrData
{
	PVOID	OldFunAddr;
	PVOID	HookFunAddr;
	FunNameID FunName;
}*LPFUNADDRDATA;

typedef struct EventData
{
	DWORD  pid;
	BYTE   Buffer[2048];
	LONGLONG   DequeID;
	ShareDllEvents  eventID;
}*LPEventData;

typedef struct ShareDllInfo 
{
	LONG  m_DllLoadNum;
	LONG  m_ProcessHookedNum;
	BOOL  m_BlockNetwork;
	DWORD m_FireWallExePID;
	LONG m_EventIDDeque;
	HWND	m_FireWallHwnd;
	DWORD m_HookedPIDList[MAX_PROCESSNUM];
	EventData m_EventDataList[MAX_PROCESSNUM];
	CMyMutex m_Mutex;
	void ClearEvents()
	{
		m_Mutex.Enter();
		for (int i=0;i<MAX_PROCESSNUM;i++)
		{
			if (m_EventDataList[i].pid==0)
			{
				m_EventDataList[i].pid=0;
				m_EventDataList[i].DequeID=0;
			}
		}
		m_Mutex.Leave();
	}
	void AddEvent(DWORD pid,ShareDllEvents evID,BYTE* data,DWORD bufSize)
	{
		m_Mutex.Enter();
		InterlockedIncrement(&m_EventIDDeque);
		EventData ed={0};
		ed.DequeID=m_EventIDDeque;
		ed.pid=pid;
		ed.eventID=evID;
		memcpy_s(ed.Buffer,2048,data,bufSize);
		for (int i=0;i<MAX_PROCESSNUM;i++)
		{
			if (m_EventDataList[i].pid==0)
			{
				m_EventDataList[i]=ed;
			}
		}
		::SendMessage(m_FireWallHwnd,WMAPIHOOK,evID,m_EventIDDeque);
		m_Mutex.Leave();
	}
	void DelEvent(LONG DequeID)
	{
		m_Mutex.Enter();
		InterlockedDecrement(&m_EventIDDeque);
		for (int i=0;i<MAX_PROCESSNUM;i++)
		{
			if (m_EventDataList[i].DequeID==DequeID)
			{
				m_EventDataList[i].pid=0;
				m_EventDataList[i].DequeID=0;
				break;
			}
			
		}
		m_Mutex.Leave();
	}
	EventData GetEventData(LONG DequeID)
	{
		m_Mutex.Enter();
		EventData ed={0};
		for (int i=0;i<MAX_PROCESSNUM;i++)
		{
			if (m_EventDataList[i].DequeID==DequeID)
			{
				ed=m_EventDataList[i];
				break;
			}
		}
		m_Mutex.Leave();
		return ed;
	}
}*LPShareDllInfo;
